<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category RoleController.php
 * @Depiction:
 * Author: hwy
 * Version: 1.0
 * Modified at: 2019/10/23  16:02
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Role\Store;
use App\Http\Requests\Role\Update;
use App\Http\Requests\RolePost;
use App\Models\Admin;
use App\Models\AdminLog;
use App\Models\Permission;
use App\Models\Role;
use App\Models\RoleHasPermission;
use App\Models\RoleUser;
use App\Services\AdminService;
use App\Services\RoleService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\View\View;

class RoleController extends Controller
{
    /**
     * @var string
     */
    protected $_model = 'role';

    /**
     * @var AdminService
     */
    protected $adminService;

    /**
     * @var RoleService
     */
    protected $roleService;

    /**
     * AdminController constructor.
     */
    public function __construct()
    {
        $this->adminService = AdminService::getInstance();
        $this->roleService  = RoleService::getInstance();
    }

    /**
     * @param Request $request
     * @return Application|Factory|View
     * @Depiction
     * @Author hwy
     * @Date 2021/3/17  14:09
     */
    public function index(Request $request)
    {
        //$permission = $permission->getTreeData('level', 'id');
        return view('admin.' . $this->_model . '.index', compact('request'));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date 2021/3/17  14:40
     */
    public function getList(Request $request)
    {
        $responseData = $this->roleService->getList($request);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @param Permission $permission
     * @return Application|Factory|View
     * @Depiction
     * @Author hwy
     * @Date 2021/3/17  14:40
     */
    public function create(Request $request, Permission $permission)
    {
        $permission = $permission->getTreeData('level', 'id');
        return view('admin.role.create', compact('permission'));
    }

    /**
     * @param Store $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date 2021/5/25  16:32
     */
    public function store(Store $request)
    {
        $responseData = $this->roleService->addData($request);
        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @param Permission $permission
     * @param $id
     * @return Application|Factory|View
     * @Depiction
     * @Author hwy
     * @Date 2021/3/17  14:41
     */
    public function edit(Request $request, Permission $permission, $id)
    {
        $permission        = $permission->getTreeData('level', 'id');
        $role              = $this->roleService->getOne($id);
        $roleHasPermission = RoleHasPermission::where('role_id', $id)->get()->pluck('permission_id')->toArray();
        return view('admin.role.edit', compact('request', 'permission', 'role', 'roleHasPermission'));
    }

    /**
     * @param Update $request
     * @param $id
     * @return array|JsonResponse
     * @Depiction
     * @Author hwy
     * @Date 2021/5/25  16:32
     */
    public function update(Update $request, $id)
    {
        if ($id == 1) {
            return return_error_msg('超级管理员不能编辑');
        }
        if (empty($request->permission_id))
            return ['code' => -1, 'message' => '请配置权限'];
        $rules     = [
            'name' => 'required|unique:roles,name,' . $request->id,
        ];
        $messages  = [
            'name.required' => '角色名称不能为空！',
            'name.unique'   => '角色名称已存在！',
        ];
        $validator = Validator::make(['name' => $request->name], $rules, $messages);
        if ($validator->fails()) {
            return ['code' => -1, 'message' => $validator->errors()->first()];
        }

        $responseData = $this->roleService->editData($request);

        return response()->json($responseData);
    }

    public function destroy(Request $request)
    {
        $ids = $request->id;

        $responseData = $this->roleService->delData($ids);
        return response()->json($responseData);
    }


}
